main.dart
import 'package:flutter/material.dart';
import 'package:b/filter_screen.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return const MaterialApp(
home: FilterScreen(),
debugShowCheckedModeBanner: false,
);
}
}
services.dart
import 'dart:convert';
import 'package:b/model.dart';
import 'package:http/http.dart' as http;
class ApiServices {
Future myFilterList() async {
Uri url = Uri.parse("https://dummyjson.com/products");
var response = await http.get(url);
try {
if (response.statusCode == 200) {
return FilterModel.fromJson(jsonDecode(response.body));
} else {
return null;
}
} catch (e) {
print(e);
}
return null;
}
}
// the error message shown due to the rating value,
// in some product rating value in the form of double in some product it's value is iintn form.
model.dart
class FilterModel {
List? products;
num? total;
num? skip;
num? limit;
FilterModel({this.products, this.total, this.skip, this.limit});
FilterModel.fromJson(Map json) {
if (json['products'] != null) {
products = [];
json['products'].forEach((v) {
products!.add(Products.fromJson(v));
});
}
total = json['total'];
skip = json['skip'];
limit = json['limit'];
}
Map toJson() {
final Map data = Map();
if (products != null) {
data['products'] = products!.map((v) => v.toJson()).toList();
}
data['total'] = total;
data['skip'] = skip;
data['limit'] = limit;
return data;
}
}
class Products {
num? id;
String? title;
String? description;
num? price;
num? discountPercentage;
dynamic rating;
num? stock;
String? brand;
String? category;
String? thumbnail;
List? images;
Products(
{this.id,
this.title,
this.description,
this.price,
this.discountPercentage,
this.rating,
this.stock,
this.brand,
this.category,
this.thumbnail,
this.images});
Products.fromJson(Map json) {
id = json['id'];
title = json['title'];
description = json['description'];
price = json['price'];
discountPercentage = json['discountPercentage'];
rating = json['rating'];
stock = json['stock'];
brand = json['brand'];
category = json['category'];
thumbnail = json['thumbnail'];
images = json['images'].cast();
}
Map toJson() {
final Map data = Map();
data['id'] = id;
data['title'] = title;
data['description'] = description;
data['price'] = price;
data['discountPercentage'] = discountPercentage;
data['rating'] = rating;
data['stock'] = stock;
data['brand'] = brand;
data['category'] = category;
data['thumbnail'] = thumbnail;
data['images'] = images;
return data;
}
}
filter_screen.dart
import 'package:b/model.dart';
import 'package:b/services.dart';
import 'package:flutter/material.dart';
class FilterScreen extends StatefulWidget {
const FilterScreen({super.key});
@override
State createState() => _FilterScreenState();
}
class _FilterScreenState extends State {
filterProduct() {
ApiServices().myFilterList().then((value) {
myItems(value!.products!);
setState(() {});
});
}
// for filtering the items
List filterLists = [];
List myItems(List list) {
for (var element in list) {
if (element.rating > 4.7 && element.rating < 5) {
// show only the items name whose rating is greate then 4.7 and less then 5
filterLists.add(element);
}
}
return filterLists;
}
@override
void initState() {
filterProduct();
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("Filter The Item's From API"),
),
body: ListView.builder(
shrinkWrap: true,
itemCount: filterLists.length,
itemBuilder: (context, index) {
return ListTile(
title: Text(
filterLists[index].title.toString(),
),
subtitle: Text(
"Rating : ${filterLists[index].rating.toString()}",
),
);
}),
);
}
}
// in ther it showt he product title whose brand is apple lets check it from postman